package tech.lq0.mineapex.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import tech.lq0.mineapex.group.ApexGroup;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

public class Syringe extends Item {

    public Syringe() {
        super(new Properties().group(ApexGroup.group));
    }

    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (playerIn.getHealth() >= playerIn.getMaxHealth()) {
            playerIn.sendStatusMessage(new StringTextComponent("当前无需治疗！"), true);
            return ActionResult.resultPass(playerIn.getHeldItem(handIn));
        }
        playerIn.setActiveHand(handIn);
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        if (entityLiving instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entityLiving;

            player.setHealth(Math.min(player.getMaxHealth(), player.getHealth() + player.getMaxHealth() * 0.25f));
            if (!player.isCreative()) {
                stack.shrink(1);
            }
            // 治疗完成音频
            player.getEntityWorld().playSound(player.getPosX(), player.getPosY(), player.getPosZ(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0F, 1.0F, true);
        }
        return stack;
    }

    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.EAT;
    }

    @Override
    @ParametersAreNonnullByDefault
    public int getUseDuration(ItemStack stack) {
        return 100;
    }
}
